package enterprise.manage.web.controller.stock;

import enterprise.manage.dao.ProductTypeDao;
import enterprise.manage.dao.StockDao;
import enterprise.manage.entity.Stocks;
import enterprise.manage.service.SysNoService;
import enterprise.manage.util.StringUtilsExt;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User:zhenmingyue
 * Date: 2012-11-25
 * Desc:
 */
public class StockHandlerController extends AbstractController {
    @Autowired
    StockDao stockDao;
    @Autowired
    ProductTypeDao productTypeDao;

    @Autowired
    SysNoService sysNoService;
    public static final String DEFAULT_PAGE = "stock/view";

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String method = StringUtilsExt.trimToEmpty(request.getParameter("method"));
        String inOrOut = StringUtilsExt.trimToEmpty(request.getParameter("inOrOut"));
        String id = StringUtilsExt.trimToEmpty(request.getParameter("id"));

        Stocks entity = getEntity(id);
        List productTypeList = productTypeDao.getProductTypeSelect();
        List productBrandList = null;
        List productModelList = null;

        String pageName;

        if ("edit".equals(method)) {
            pageName = editPage(request);
//            entity.getInProducts();
//            entity.getOutProducts();
//            productBrandList = productTypeDao.getProductBrandSelect(entity.getProduct().getType());
//            productModelList = productTypeDao.getProductModelSelect(entity.getProduct().getType(), entity.getProduct().getBrand());

        } else if ("add".equals(method)) {
            pageName = add(request);
            productBrandList = new ArrayList();
            productModelList = new ArrayList();

            entity.setInOrOut(inOrOut);
            entity.setStockNo(sysNoService.getNo("S"));
        } else {
            pageName = view();
        }
        request.setAttribute("productTypeList", productTypeList);
        request.setAttribute("productBrandList", productBrandList);
        request.setAttribute("productModelList", productModelList);

        request.setAttribute("inOrOut", inOrOut);
        request.setAttribute("method", method);
        request.setAttribute("entity", entity);
        return new ModelAndView(pageName);
    }

    public Stocks getEntity(String id) {
        if (StringUtils.isNotBlank(id)) {
            return stockDao.findByUnique("id", Long.parseLong(id));
        } else {
            return new Stocks();
        }
    }

    /**
     * 初始化 编辑页面
     *
     * @param request
     * @return
     */
    public String editPage(HttpServletRequest request) {
        return "stock/edit";
    }

    /**
     * 初始化添加页面
     *
     * @param request
     * @return
     */
    public String add(HttpServletRequest request) {
        return "stock/edit";
    }

    /**
     * 初始化详情页面
     *
     * @return
     */
    public String view() {
        return "stock/view";
    }

}